
package com.trade.core.message;

import com.trade.core.utils.TradeCommonUtils;
import org.springframework.http.HttpStatus;

/**
 * Created by Ace on 2017/6/11.
 */
public class TradeObjectRestResponse<T> extends TradeBaseResponse {

    T data;
    //成功状态 ：200（默认，表示业务正常）
    public static final int STATUS_SUCCESS = 200;
    //服务器错误
    public static final int STATUS_FAIL = 500;
    //400（业务错误）
    public static final int STATUS_ERROR_BIZ = 400;

    public TradeObjectRestResponse() {

    }

    public TradeObjectRestResponse(int status, String message) {
        super(status, message);
    }

    public TradeObjectRestResponse(int status, String message, T data) {
        super.setStatus(status);
        super.setMessage(message);
        this.setData(data);
    }

    public static <T> TradeObjectRestResponse<T> success(T data) {
        return new TradeObjectRestResponse<T>(STATUS_SUCCESS, HttpStatus.OK.name(), data);
    }

    public static <T> TradeObjectRestResponse<T> error(String message) {
        return new TradeObjectRestResponse<>(STATUS_ERROR_BIZ, message, null);
    }

    public static <T> TradeObjectRestResponse<T> error(int status, String message) {
        return new TradeObjectRestResponse<T>(status, message);
    }

    public TradeObjectRestResponse data(T data) {
        this.setData(data);
        return this;
    }

    public T getData() {
        return data;
    }

    public T checkAndGetData() {
        TradeCommonUtils.dealResponse(this);
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


}
